import axios from 'common/js/token/request'

//  add to cart
export function addToCart(sku, qty) {
  const url = '/api/ShoppingCart'
  const data = {
    sku,
    qty
  }
  return axios.post(url, data)
    .then((res) => {
      return Promise.resolve(res.data)
    })
    .catch((err) => {
      console.log(err)
      return Promise.reject(err)
    })
}

// get cart-list  from server
export function getCartList() {
  const url = 'api/ShoppingCart/promotion'
  return axios.get(url)
    .then((res) => {
      return Promise.resolve(res.data)
    })
    .catch(err => {
      console.log(err)
    })
}

// delete cart-item from cart-list
export function removeCartItem(sku) {
  const url = '/api/ShoppingCart/promotion/' + sku
  return axios.delete(url)
    .then((res) => {
      return Promise.resolve(res.data)
    })
    .catch(error => {
      console.log(error)
    })
}

// create order
export function creatOrder(params) {
  const url = '/api/Orders/promotion';
  return axios.post(url,params)
    .then((res) => {
      return Promise.resolve(res.data)
    })
    .catch(error => {
      console.log(error)
    })
}


/**
 * 校验单个商品的库存
 *  use :  addToCart  addnum reducenum (减的时候好像不用)
 */
export function checkStock (sku) {
  const url = `/api/Products/${sku}/stockQtyCheck`
  return axios.post(url, {})
    .then((response) => {
      return Promise.resolve(response.data)
    })
    .catch(error => {
      console.log(error)
    })
}

